package org.example.controller;

import org.example.annotation.TokenRequired;
import org.example.entity.CartEntity;
import org.example.entity.CartVoEntity;
import org.example.entity.UserEntity;
import org.example.service.ICartService;
import org.example.util.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hanjiafeng
 * @date 2021/4/20 11:24
 * @desc todo
 */
@RestController
@RequestMapping("cart")
public class CartController {
    private final ICartService cartService;

    public CartController(ICartService cartService) {
        this.cartService = cartService;
    }

    @TokenRequired
    @PostMapping("list")
    public ResponseEntity<List<CartVoEntity>> list(UserEntity userEntity) {
        ResponseEntity<List<CartVoEntity>> responseEntity;
        List<CartVoEntity> cartVoEntityList = cartService.listCart(userEntity.getUid());
        if (!cartVoEntityList.isEmpty()) {
            responseEntity = ResponseEntity.success("查询购物车成功");
            responseEntity.setData(cartVoEntityList);
        } else {
            responseEntity = ResponseEntity.failure(ResponseEntity.Code.CART_EMPTY, "购物车为空");
        }
        return responseEntity;
    }

    @TokenRequired
    @PostMapping("findByCids")
    public ResponseEntity<List<CartVoEntity>> findByCids(String cids) {
        ResponseEntity<List<CartVoEntity>> responseEntity;
        List<CartVoEntity> cartVoEntityList = cartService.findByCids(cids);
        if (!cartVoEntityList.isEmpty()) {
            responseEntity = ResponseEntity.success("查询购物车成功");
            responseEntity.setData(cartVoEntityList);
        } else {
            responseEntity = ResponseEntity.failure(ResponseEntity.Code.CART_EMPTY, "购物车为空");
        }
        return responseEntity;
    }

    @TokenRequired
    @PostMapping("save")
    public ResponseEntity<Void> save(Integer pid, Integer num, UserEntity userEntity) {
        CartEntity cartEntity = new CartEntity();
        cartEntity.setNum(num);
        cartEntity.setPid(pid);
        cartEntity.setUid(userEntity.getUid());
        cartService.save(cartEntity, userEntity);
        return ResponseEntity.success("添加购物车成功");
    }

    @TokenRequired
    @PostMapping("changeNum")
    public ResponseEntity<Void> changeNum(Integer cid, Integer num) {
        ResponseEntity<Void> responseEntity;
        int count = cartService.changeNum(cid, num);
        if (count == 1) {
            responseEntity = ResponseEntity.success("更改购物车成功");
        } else {
            responseEntity = ResponseEntity.failure("更改购物车商品数量失败,请联系管理员");
        }
        return responseEntity;
    }

    @TokenRequired
    @PostMapping("remove")
    public ResponseEntity<Void> remove(Integer cid) {
        int count = cartService.delete(cid);
        if (count == 1) {
            return ResponseEntity.success("删除成功");
        } else {
            return ResponseEntity.failure("删除失败");
        }
    }

    @TokenRequired
    @PostMapping("removeList")
    public ResponseEntity<Void> removeList(String cids) {
        String[] cidArray = cids.split(",");
        ResponseEntity<Void> responseEntity = ResponseEntity.success("删除成功");
        int count = 0;
        for (String cid : cidArray) {
            Integer cidInt = Integer.parseInt(cid);
            ResponseEntity<Void> tempResponseEntity = remove(cidInt);
            if (tempResponseEntity.getCode() == 1000) {
                count++;
            }
        }
        if (count == 0) {
            responseEntity = ResponseEntity.failure("删除失败");
        } else if (count != cidArray.length) {
            responseEntity = ResponseEntity.success("删除部分成功");
        }
        return responseEntity;
    }
}
